// ignore_for_file: unrelated_type_equality_checks

import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:provider/provider.dart';

class WifiSettingDialogMWidget extends StatelessWidget {
  const WifiSettingDialogMWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: WifiSettingDialogM(),
    );
  }
}

class WifiSettingDialogM extends StatefulWidget {
  WifiSettingDialogM({super.key});

  @override
  State<WifiSettingDialogM> createState() => _WifiSettingDialogMState();
}

class _WifiSettingDialogMState extends State<WifiSettingDialogM> {
  var _montageProvider = MontageProvider();
  TextEditingController ssidvc = TextEditingController();
  TextEditingController pwdvc = TextEditingController();
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    print("========>${_montageProvider.wifiSetting.length}");
    return Container(
      width: 320.0.W,
      height: 396.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "WiFi配置",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 20.0.H,
                ),
                GridView.builder(
                  padding: EdgeInsets.zero,
                  scrollDirection: Axis.vertical,
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: _montageProvider.wifiSetting.length,
                  itemBuilder: (con, index) {
                    return wifiItem(
                      _montageProvider.wifiSetting[index]["title"],
                      _montageProvider.wifiSetting[index]["isSelect"],
                      index,
                    );
                  },
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 5,
                    crossAxisSpacing: 1,
                    mainAxisSpacing: 10,
                    //宽高比
                    childAspectRatio: 1,
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(top: 5.0.H),
                  child: Stack(
                    children: [
                      Positioned(
                        top: 12,
                        child: Text(
                          'SSID',
                          style: StandardTextStyle.textWhite17Style,
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                        height: 50.0.W,
                        child: TextField(
                            style: const TextStyle(color: Colors.white),
                            controller: ssidvc,
                            decoration: const InputDecoration(
                              enabledBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue),
                              ),
                              focusedBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue),
                              ),
                            ),
                            autofocus: true,
                            cursorColor: ColorPlate.white),
                      ),
                    ],
                  ),
                ),
                const Divider(
                  height: 1.0,
                  color: ColorPlate.white, // 分割线颜色
                ),
                Container(
                  padding: EdgeInsets.only(top: 10.0.W),
                  child: Stack(
                    children: [
                      Positioned(
                        top: 12,
                        child: Text(
                          '密码',
                          style: StandardTextStyle.textWhite17Style,
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(50, 0, 0, 0),
                        height: 50.0.W,
                        child: TextField(
                            style: const TextStyle(color: Colors.white),
                            controller: pwdvc,
                            decoration: const InputDecoration(
                              enabledBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue),
                              ),
                              focusedBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.blue),
                              ),
                            ),
                            autofocus: true,
                            cursorColor: ColorPlate.white),
                      ),
                    ],
                  ),
                ),
                const Divider(
                  height: 1.0,
                  color: ColorPlate.white, // 分割线颜色
                )
              ],
            ),
          ),
          Column(
            children: [
              Container(
                padding: EdgeInsets.only(top: 15.0.W),
                child: Divider(
                  height: 1.0.W,
                  color: ColorPlate.white, // 分割线颜色
                ),
              ),
              Container(
                height: 50.0.W,
                child: Row(
                  children: [
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          // widget.vc.text = '';
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: Text(
                            '取消',
                            style: StandardTextStyle.textWhite16Style,
                          ),
                        ),
                      ),
                    ),
                    Container(
                      width: 0.5.W,
                      height: 50.0.W,
                      color: ColorPlate.white,
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          // widget.okBtnTap();
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: Text(
                            '配置',
                            style: StandardTextStyle.textWhite16Style,
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget wifiItem(String title, bool selState, int index) {
    return Stack(
      children: [
        Container(
          margin: const EdgeInsets.all(10),
          padding: const EdgeInsets.all(5),
          width: 60.0.W,
          height: 60.0.W,
          decoration: BoxDecoration(
            //设置边框
            border: Border.all(color: Colors.white, width: 1),
            //背景颜色
            color: ColorPlate.backgroundOpacity0,
            //设置圆角
            borderRadius: BorderRadius.circular((5.0)),
          ),
          child: Center(
            child: Text(title,
                style: StandardTextStyle.textWhite12Style,
                textAlign: TextAlign.center,
                maxLines: 2,
                overflow: TextOverflow.ellipsis),
          ),
        ),
        Positioned(
          top: 0,
          right: 0,
          child: InkWell(
            onTap: () {
              _montageProvider.changeWIFISettingState(index);
            },
            child: Image.asset(
              height: 20.0.H,
              width: 20.0.W,
              selState == true
                  ? AssetUtils.getAsset2Image("m_wifi_select")
                  : AssetUtils.getAsset2Image("m_wifi_noselect"),
            ),
          ),
        )
      ],
    );
  }
}
